<?php
/**
 * User: xuanbang.wen
 * Date: 2019/8/16
 */

namespace App\Web\Controller;

use App\Facades\Dict;
use App\Service\ExternalAPI\VApi;
use App\Facades\UrlCrypt;
use App\Service\FileHandler;
use App\Web\Facades\UserAuth;
use App\Web\Model\Attach;
use App\Web\Model\CaseModel;
use App\Web\Model\Manager;
use App\Web\Model\ResumeRemark;
use App\Web\Model\ResumeSearch;
use App\Web\Model\ResumeTag;
use App\Web\Model\CaseResume;
use EchoPHP\Facades\Redis;
use EchoPHP\Facades\Validation;
use EchoPHP\Http\Request;
use App\Web\Model\Resume as ResumeModel;
use EchoPHP\Http\Response\JsonResponse;
use EchoPHP\Validator\Validator;
use Exception;

class Resume extends Controller {

    /**
     * 简历库-简历搜索
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function index(Request $request) {
        $oprId = $this->getLoginId();
        $params = $request->getPost();
        $pageNum = $request->getParam('pagenum', 1);
        $perPageCount = $request->getParam('pagelimit', 10);
        $resumeSearchModel = new ResumeSearch();

        $searchResult = $resumeSearchModel->getResumeList($oprId, $params, $pageNum, $perPageCount);

        //关键字标红
        $searchKeyStr = trim($request->getPost('fulltext', '')
            . ' ' . $request->getPost('cotext', '')
            . ' ' . $request->getPost('jobtext', '')
        );

        if ($searchKeyStr != '') {
            $searchKeyStr = str_replace(array('，', ',', '；', ';', '、', '　'), ' ', $searchKeyStr);
            $searchKeyArr = explode(' ', $searchKeyStr);
            $searchKey = [];
            foreach ($searchKeyArr as $item) {
                if (trim($item) != '') {
                    $searchKey[] = array('name' => $item, 'score' => mb_strlen($item, 'utf-8'));
                }
            }
            $score = [];
            foreach ($searchKey as $key => $value) {
                $score[$key] = $value['score'];
            }
            array_multisort($score, SORT_DESC, $searchKey);
            $searchResult['searchkey'] = [];
            foreach ($searchKey as $key => $value) {
                $searchResult['searchkey'][] = $value['name'];
            }
        }

        return $this->json(['status' => true, 'message' => '查询成功', 'searchresult' => $searchResult]);
    }

    /**
     * 根据批量简历令牌，换取每个简历的Token
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function getResumeToken(Request $request) {
        $oprId = $this->getLoginId();
        $serial = $request->getParam('serial', '');
        $listkey = $request->getParam('listkey', '');
        $listtoken = $request->getParam('listtoken', '');

        $resumeidArr = explode(",", $listkey);
        $serial = trim($serial, ',');
        $serialArr = explode(",", $serial);
        //$serialArr = array_filter(explode(",", $serial));
        if (empty($serialArr)
            || count($serialArr) > count($resumeidArr)
            || count($resumeidArr) > 200
            || $listtoken != UrlCrypt::generateToken($resumeidArr)) {
            $resultBody['status'] = false;
            $resultBody['message'] = '未查到对应记录';
            return $this->json($resultBody);
        }

        $resultBody = [];
        $resultBody['status'] = true;
        $resultBody['message'] = '';
        $resultBody['serial'] = $serial;
        $resultBody['listkey'] = $listkey;
        $resultBody['listtoken'] = $listtoken;
        $resumeIds = [];
        $resumeTokens = [];
        foreach ($serialArr as $item) {
            if (!isset($resumeidArr[$item])) {
                continue;
            }
            $resumeIds[] = $resumeidArr[$item];
            $resumeTokens[$resumeidArr[$item]] = UrlCrypt::generateToken([$resumeidArr[$item]]);
        }

        $managerModel = new Manager();
        $field = 'userid51job as resumeid,relationaccid as accountid,managerid,cname,workyear,gender';
        $info = $managerModel->getManagerListByResumeId($resumeIds, $field);
        $info = $managerModel->chkDownload($oprId, $info);
        foreach ($info as $item) {
            $item['token'] = $resumeTokens[$item['resumeid']];
            $resultBody['resumeToken'][] = $item;
        }

        return $this->json($resultBody);
    }

    /**
     * 简历库列表详情接口
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function simple(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid', '');
        $token = $request->getParam('token', '');
        $channel = $request->getParam('channel', '');

        if ($token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '未查到对应记录';
            return $this->json($resultBody);
        }

        $resumeModel = new ResumeModel();
        $resumeIds = [$resumeId];
        $resumeModel->arrValueToKey($resumeIds);
        $resultBody['resumeList'] = $resumeModel->getSimpleResumeInfo($resumeIds, $oprId, $channel);
        $resultBody['status'] = true;
        if (empty($resultBody['resumeList'])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '未查到对应记录';
        }
        return $this->json($resultBody);
    }

    /**
     * 下载简历
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function downloadResume(Request $request) {
        $oprId = $this->getLoginId();
        $resumeIds = $request->getPost('resumeids', '');
        $resumeIds = explode(',', $resumeIds);

        $resumeModel = new ResumeModel();
        $result = $resumeModel->downloadResume($oprId, $resumeIds);
        return $this->json($result);
    }

    /**
     * 简历数据
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function resumeShow(Request $request) {
        $oprId = $this->getLoginId();
        $resumeIds = $request->getParam('resumeid', '');
        $channel = $request->getParam('channel', '');
        $token = $request->getParam('token', '');
        $searchKeyStr = $request->getParam('searchkey', '');
        //$resumeIds = '301809654';   //300022254    //301809654 郭子恒

        //$resumeIds = '2501981194';
        //$resumeIds_51['301809654'] = array('accountid' => 51911892, 'resumeid' => 301809654);

        if (empty($resumeIds)) {
            $resultBody['status'] = false;
            $resultBody['message'] = '未查到对应记录';
            return $this->json($resultBody);
        }
        $resumeIds = explode(',', $resumeIds);
        if (count($resumeIds) != 1) {
            $resultBody['status'] = false;
            $resultBody['message'] = '未查到对应记录';
            return $this->json($resultBody);
        }

        if (empty($token) || empty($resumeIds[0]) || $token != UrlCrypt::generateToken([$resumeIds[0]])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '未查到对应记录';
            return $this->json($resultBody);
        }

        //简历详情查询关键字标红
        if ($searchKeyStr != '') {
            $searchKeyStr = str_replace(array('，', ',', '；', ';', '、', '　'), ' ', $searchKeyStr);
            $searchKeyArr = explode(' ', $searchKeyStr);
            $searchKey = [];
            foreach ($searchKeyArr as $item) {
                if (trim($item) != '') {
                    $searchKey[] = array('name' => $item, 'score' => mb_strlen($item, 'utf-8'));
                }
            }
            $score = [];
            foreach ($searchKey as $key => $value) {
                $score[$key] = $value['score'];
            }
            array_multisort($score, SORT_DESC, $searchKey);
            $resultBody['searchkey'] = [];
            foreach ($searchKey as $key => $value) {
                $resultBody['searchkey'][] = $value['name'];
            }
        }

        $resumeModel = new ResumeModel();

        //简历已读记录
        $redisObj = Redis::connect('readresume');
        $nowKey = $resumeModel->getReadResumeKey(false);
        @$redisObj->HMSET('rd_' . $oprId . '_' . $nowKey['now'], array($resumeIds[0] => time()));

        $resumeIds = [$resumeIds[0]];

        $resumeModel->arrValueToKey($resumeIds);
        $resultBody['status'] = true;
        $resultBody['resumeList'] = $resumeModel->getResumeInfo($resumeIds, $oprId, $channel);
        return $this->json($resultBody);
    }

    /**
     * 获取简历头像
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function resumeAvatar(Request $request) {
        $resumeId = $request->getParam('resumeid', '');
        $accountId = $request->getParam('accountid', '');
        $platform = $request->getParam('platform', '');
        $gender = $request->getParam('gender', '');

        //$resumeId = '300022254';
        //$accountId = '49985251';

        //$resumeId = '301809654';
        //$accountId = '51911892';

        $fileContent = '';
        if ($platform == 'rpo' && $resumeId >= 2400000000) {
            //检测简历是否是RPO简历
            $resumeModel = new ResumeModel();
            $result = $resumeModel->getResumeBaseInfo($resumeId, 'headpath');
            if (!empty($result) && !empty($result['headpath'])) {
                $fileContent = @file_get_contents(FILESERVER . '/' . $result['headpath']);
            }
        } elseif (!empty($resumeId) && !empty($accountId) && $resumeId < 2400000000) {
            $vapi = new VApi();
            $logo = $vapi->getResumeAvatar($accountId, $resumeId);
            if (!empty($logo) && strlen($logo) > 100) {
                $fileContent = base64_decode(preg_replace('/^\s*data:image\/(?:jpeg|jpg|png);base64,\s*(?=[A-Za-z0-9+\/])(?!\s)/', '', $logo, 1));
            }
        }

        header("Content-Type: application/octet-stream");
        //请求失败或者没有头像时使用默认头像
        if (empty($fileContent)) {
            if ($gender == '女' || strtolower($gender) == 'female') {
                $fileContent = @file_get_contents(ROOT_PATH . '/app/Command/ExportFile/resource/woman.png');
            } else {
                $fileContent = @file_get_contents(ROOT_PATH . '/app/Command/ExportFile/resource/man.png');
            }
        }

        echo $fileContent;
    }

    /**
     * 获取简历附件
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function resumeAttachment(Request $request) {
        $vapi = new VApi();
        $resumeId = $request->getParam('resumeid', '');
        $accountId = $request->getParam('accountid', '');
        $attachId = $request->getParam('attachid', '');

        /*
        $resumeId = 301809654;
        $accountId = 51911892;
        $attachId = 8244043;
        */
        if (empty($resumeId) || empty($accountId) || empty($attachId)) {
            exit();
        }

        $attInfo = $vapi->getResumeAttachment($accountId, $resumeId, $attachId);
        $fileContent = $attInfo[0]["attachfile"];
        $fileContent = base64_decode(preg_replace('/data:image\/[jpeg|jpg|png]+;base64,/', '', $fileContent));
        header("Content-Type: application/octet-stream");
        echo $fileContent;
    }


    public function tagThink(Request $request) {
        $keyWord = $request->getParam('keyword', '');
        if (!$keyWord) {
            return $this->json(
                [
                    'status' => true,
                    'message' => '获取失败',
                    'taglist' => []
                ]
            );
        }
        $allTag = Dict::all('resumetag');
        $result = [];
        foreach ($allTag as $item) {
            if (stripos($item, $keyWord) !== false) {
                $result[] = $item;
            }
        }
        return $this->json(
            [
                'status' => true,
                'message' => '获取成功',
                'taglist' => $result
            ]
        );
    }

    /**
     * Notes:展示简历下评语
     * @param Request $request
     * @return JsonResponse
     */
    public function showRemarks(Request $request) {
        $resumeId = $request->getParam('resumeId');
        $myRemark = $request->getParam('myremark', 0);
        $remarks = (new ResumeRemark())->show($resumeId, $myRemark);
        return $this->json(
            [
                'status' => true,
                'message' => '获取成功',
                'remarkList' => $remarks
            ]
        );
    }

    /**
     * Notes:修改简历下评语
     * @param Request $request
     * @return JsonResponse
     */
    public function editRemarks(Request $request) {
        $params = [
            'userid' => $this->getLoginId(),
            'remarkId' => $request->getPost('remarkId'),
            'content' => $request->getPost('content'),
        ];
        $length = mb_strlen((string)$params['content']);
        if (500 < $length || $length < 3) {
            return $this->json([
                'status' => false,
                'message' => '评语需3至500字'
            ]);
        }
        if (UserAuth::getLoginId() != $request->getPost('userid')) {
            return $this->json(
                [
                    'status' => false,
                    'message' => '非创建人无法修改'
                ]
            );
        }
        $result = (new ResumeRemark())->edit($params);
        return $this->json(
            [
                'status' => $result > 0,
                'message' => $result > 0 ? '修改成功' : '修改失败'
            ]
        );

    }

    /**
     * Notes:为简历添加评语
     * @param Request $request
     * @return JsonResponse
     */
    public function addRemarks(Request $request) {
        $params = [
            'userid' => $this->getLoginId(),
            'resumeId' => $request->getPost('resumeId'),
            'content' => $request->getPost('content'),
            'caseid' => $request->getPost('caseid', 0),
        ];
        $length = mb_strlen((string)$params['content']);
        if (500 < $length || $length < 3) {
            return $this->json([
                'status' => false,
                'message' => '评语需3至500字'
            ]);
        }
        $result = (new ResumeRemark())->create($params);
        return $this->json([
            'status' => $result == 1,
            'message' => $result == 1 ? '添加成功' : '添加失败'
        ]);
    }

    /**
     * Notes:评语删除
     * @param Request $request
     * @return JsonResponse
     */
    public function softDelRemarks(Request $request) {
        if ($this->getLoginId() != $request->getPost('userid')) {
            return $this->json(
                [
                    'status' => false,
                    'message' => '非创建人无法删除'
                ]
            );
        }
        $remarkId = $request->getParam('remarkId');
        $result = (new ResumeRemark())->softDelete($remarkId);
        return $this->json(
            [
                'status' => $result == 1,
                'message' => $result == 1 ? '删除成功' : '删除失败'
            ]
        );
    }

    /**
     * Notes:展示简历下标签
     * @param Request $request
     * @return JsonResponse
     */
    public function showTags(Request $request) {
        $resumeId = $request->getParam('resumeId');
        $tags = (new ResumeTag())->show($resumeId);
        return $this->json(
            [
                'status' => true,
                'message' => '获取成功',
                'tagList' => $tags
            ]
        );
    }

    /**
     * Notes:添加简历标签
     * @param Request $request
     * @return JsonResponse
     */
    public function addTags(Request $request) {
        $params = [
            'userid' => $this->getLoginId(),
            'resumeId' => $request->getPost('resumeId'),
            'content' => $request->getPost('content'),
        ];
        $length = mb_strlen((string)$params['content']);
        if (20 < $length || $length < 3) {
            return $this->json([
                'status' => false,
                'message' => '标签需3至20字'
            ]);
        }
        $result = (new ResumeTag())->create($params);
        return $this->json([
            'status' => $result == 1,
            'message' => $result == 1 ? '添加成功' : '此标签已存在无需添加'
        ]);
    }

    /**
     * Notes:简历标签删除
     * @param Request $request
     * @return JsonResponse
     */
    public function softDelTags(Request $request) {
        $tagId = $request->getParam('tagId');
        $result = (new ResumeTag())->softDelete($tagId);
        return $this->json([
            'status' => $result == 1,
            'message' => $result == 1 ? '删除成功' : '删除失败'
        ]);
    }

    /**
     * 上传附件
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function addAttach(Request $request) {
        $resumeModel = new ResumeModel();
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid/d', 0);
        $file = $request->getFile('attach');
        $fileType = $request->getParam('attachtype', '02');
        $result = $resumeModel->addAttach($file, $oprId, $resumeId, $fileType);
        if (!$result['status'] && isset($result['validateFailure'])) {
            return $this->json([
                'status' => false,
                'message' => '文件上传错误，' . current($result['validateFailure'])
            ]);
        }
        return $this->json($result);
    }

    /**
     * Notes:简历附件列表
     * @param Request $request
     * @return JsonResponse
     */
    public function showAttach(Request $request) {
        $resumeId = $request->getParam('resumeid');
        $resumeattach = (new ResumeModel())->showAttach($resumeId);
        if (!$resumeattach) {
            return $this->json(
                [
                    'status' => true,
                    'message' => '无附件',
                    'attachList' => [],
                ]
            );
        }
        $attachids = array_column($resumeattach, 'attachid');
        $attachInfo = (new Attach())->batchGetAttach($attachids, 'attachid,attachname');
        $attach = [];
        foreach ($attachInfo as $item) {
            $attach[$item['attachid']] = $item['attachname'];
        }
        foreach ($resumeattach as $k => $v) {
            $resumeattach[$k]['attachname'] = $attach[$v['attachid']];
            unset($resumeattach[$k]['attachid']);
        }
        return $this->json(
            [
                'status' => true,
                'message' => '获取成功',
                'attachList' => $resumeattach,
            ]
        );
    }

    // 下载简历附件
    public function downloadResumeAttach(Request $request) {
        $resumeAttachId = $request->getParam('resumeattachid');
        $userId = $request->getParam('userid');
        $file = (new ResumeModel())->getAttachPath($resumeAttachId);
        if (empty($file)) {
            return $this->json(
                ['status' => false,
                    'message' => '附件不存在'
                ]
            );
        }

        $user = UserAuth::getLoginInfo();
        if ($user['issuper'] != 1 && $file['creator'] != $userId) {
            return $this->json(
                ['status' => false,
                    'message' => '附件无权下载'
                ]
            );
        }

        $filePath = FILESERVER . '/' . $file['filepath'];
        if (!$file || !file_exists($filePath)) {
            return $this->json(
                [
                    'status' => false,
                    'message' => '下载错误'
                ]
            );
        }
        return response()->download($filePath, '简历附件-' . $file['attachname']);
    }

    //删除简历附件
    public function delResumeAttach(Request $request) {
        $resumeAttachId = $request->getParam('resumeattachid');
        if (UserAuth::getLoginId() != $request->getParam('userid')) {
            return $this->json(
                [
                    'status' => false,
                    'message' => '非创建人无法删除'
                ]
            );
        }
        $result = (new ResumeModel())->softDelResumeAttach($resumeAttachId);
        return $this->json(
            [
                'status' => $result == 1,
                'message' => $result == 1 ? '删除成功' : '删除失败'
            ]
        );
    }

    /**
     * Notes:当前用户所对接此简历的职位列表
     * @param Request $request
     * @return JsonResponse
     */
    public function myCaseList(Request $request) {
        $resumeId = $request->getParam('resumeid');
        $userid = $this->getLoginId();
        $caseList = (new CaseResume())->getCaseList($resumeId, $userid, true, true);
        return $this->json([
            'status' => true,
            'message' => '获取成功',
            'caseList' => $caseList,
        ]);
    }

    /**
     * Notes:其他（非当前）所对接此简历的职位列表
     * @param Request $request
     * @return JsonResponse
     */
    public function otherCaseList(Request $request) {
        $resumeId = $request->getParam('resumeid');
        $userid = $this->getLoginId();
        $caseList = (new CaseResume())->getCaseList($resumeId, $userid, false);
        return $this->json([
            'status' => true,
            'message' => '获取成功',
            'caseList' => $caseList,
        ]);
    }

    public function allCaseList(Request $request) {
        $resumeId = $request->getParam('resumeid');
        $caseList = (new CaseResume())->getCaseList($resumeId);
        return $this->json([
            'status' => true,
            'message' => '获取成功',
            'caseList' => $caseList,
        ]);
    }

    /**
     * Notes:简历对应职位的流程变更记录
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function caseTagList(Request $request) {
        $subId = $request->getParam('subid');
        $keyCode = $request->getParam('keycode');
        $caseResumeTag = (new CaseResume())->getResumeTagHis($subId, $keyCode);
        return $this->json(
            [
                'status' => true,
                'message' => '获取成功',
                'caseTagList' => $caseResumeTag,
            ]
        );
    }


    /**
     * Notes:新增简历基本信息
     * @param Request $request
     * @return JsonResponse
     */
    public function addResume(Request $request) {
        $oprId = $this->getLoginId();
        $resumeInfo = [];
        $fieldName = ['cname', 'gender', 'phone', 'email', 'birthday', 'workyear', 'area', 'situation'];
        foreach ($fieldName as $item) {
            $resumeInfo[$item] = '';
            if ($request->getParam($item) !== NULL) {
                $resumeInfo[$item] = $request->getParam($item);
            }
        }

        $resumeModel = new ResumeModel();
        /*
        $resumeInfo['cname'] = '张三';
        $resumeInfo['gender'] = '1';
        $resumeInfo['phone'] = '13601750981';
        $resumeInfo['email'] = 'ziheng.guo@51job.com';
        $resumeInfo['birthday'] = '2018-07-09';
        $resumeInfo['workyear'] = '2015';
        $resumeInfo['area'] = '020000';
        $resumeInfo['situation'] = '2';
        */
        $result = $resumeModel->saveNewResume($oprId, $resumeInfo);

        if (!$result['status']) {
            return $this->json($result);
        }

        $resumeId = $result['resumeid'];
        $caseId = $request->getParam('caseid');
        if (!empty($caseId)) {
            $caseModel = new CaseModel();
            $addCaseResult = $caseModel->addToCase($oprId, $caseId, [$resumeId]);
        } else {
            $caseId = '';
        }

        $result['caseid'] = $caseId;
        $result['token'] = UrlCrypt::generateToken([$resumeId]);
        return $this->json($result);
    }

    /**
     * Notes:编辑简历基本信息
     * @param Request $request
     * @return JsonResponse
     */
    public function editResume(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid', '');
        $token = $request->getParam('token', '');

        if (empty($resumeId) || empty($token) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $resumeInfo = [];
        $fieldName = ['cname', 'gender', 'phone', 'email', 'birthday', 'workyear', 'area', 'expectarea', 'salaryfrom', 'salaryto',
            'expectsalaryfrom', 'expectsalaryto', 'workindustry', 'position', 'selfintro', 'jobterm', 'situation', 'entrytime',
            'nofocuse', 'skillinfo', 'awardinfo', 'otherinfo'];
        foreach ($fieldName as $item) {
            if ($request->getParam($item) !== NULL) {
                $resumeInfo[$item] = $request->getParam($item);
            }
        }

        if ($request->getParam('jobterm') === NULL && $request->getParam('seektype') !== NULL) {
            $resumeInfo['jobterm'] = $request->getParam('seektype');
        }

        $resumeModel = new ResumeModel();
        //$oprId = 9;
        //$resumeId = 2501982375;
        $result = $resumeModel->saveEditResume($oprId, $resumeId, $resumeInfo);
        return $this->json($result);
    }

    /**
     * Notes:删除简历工作经历信息
     * @param Request $request
     * @return JsonResponse
     */
    public function delResumeWork(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid');
        $token = $request->getParam('token', '');
        $id = $request->getParam('id', '');

        if (empty($resumeId) || empty($token) || empty($id) || !is_numeric($id) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $resumeModel = new ResumeModel();
        //$oprId = 9;
        //$resumeId = 2501982375;
        $result = $resumeModel->delResumeWork($oprId, $resumeId, $id);
        return $this->json([
            'status' => $result == 1,
            'message' => $result == 1 ? '删除成功' : '删除失败'
        ]);
    }

    /**
     * Notes:删除简历项目经验信息
     * @param Request $request
     * @return JsonResponse
     */
    public function delResumeProject(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid');
        $token = $request->getParam('token', '');
        $id = $request->getParam('id', '');

        if (empty($resumeId) || empty($token) || empty($id) || !is_numeric($id) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $resumeModel = new ResumeModel();
        //$oprId = 9;
        //$resumeId = 2501982375;
        $result = $resumeModel->delResumeProject($oprId, $resumeId, $id);
        return $this->json([
            'status' => $result == 1,
            'message' => $result == 1 ? '删除成功' : '删除失败'
        ]);
    }

    /**
     * Notes:删除简历教育经历信息
     * @param Request $request
     * @return JsonResponse
     */
    public function delResumeEdu(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid');
        $token = $request->getParam('token', '');
        $id = $request->getParam('id', '');

        if (empty($resumeId) || empty($token) || empty($id) || !is_numeric($id) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $resumeModel = new ResumeModel();
        //$oprId = 9;
        //$resumeId = 2501982375;
        $result = $resumeModel->delResumeEdu($oprId, $resumeId, $id);
        return $this->json([
            'status' => $result == 1,
            'message' => $result == 1 ? '删除成功' : '删除失败'
        ]);
    }

    /**
     * Notes:编辑简历工作经历信息
     * @param Request $request
     * @return JsonResponse
     */
    public function editResumeWork(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid');
        $token = $request->getParam('token', '');
        $id = $request->getParam('id', '');

        if (empty($resumeId) || empty($token) || !is_numeric($id) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $itemInfo = [];
        $fieldName = ['starttime', 'endtime', 'company', 'duty', 'salary', 'content', 'workindustry'];

        foreach ($fieldName as $item) {
            $itemInfo[$item] = '';
            if ($request->getParam($item) !== NULL) {
                $itemInfo[$item] = $request->getParam($item);
            }
        }

        $resumeModel = new ResumeModel();

        $result = $resumeModel->saveResumeWork($oprId, $resumeId, $id, $itemInfo);
        return $this->json($result);
    }

    /**
     * Notes:编辑简历项目经验信息
     * @param Request $request
     * @return JsonResponse
     */
    public function editResumeProject(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid');
        $token = $request->getParam('token', '');
        $id = $request->getParam('id', '');

        if (empty($resumeId) || empty($token) || !is_numeric($id) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $itemInfo = [];
        $fieldName = ['starttime', 'endtime', 'company', 'duty', 'project', 'content'];

        foreach ($fieldName as $item) {
            $itemInfo[$item] = '';
            if ($request->getParam($item) !== NULL) {
                $itemInfo[$item] = $request->getParam($item);
            }
        }

        $resumeModel = new ResumeModel();

        $result = $resumeModel->saveResumeProject($oprId, $resumeId, $id, $itemInfo);
        return $this->json($result);
    }


    /**
     * Notes:编辑简历教育经历信息
     * @param Request $request
     * @return JsonResponse
     */
    public function editResumeEdu(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid');
        $token = $request->getParam('token', '');
        $id = $request->getParam('id', '');

        if (empty($resumeId) || empty($token) || !is_numeric($id) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $itemInfo = [];
        $fieldName = ['starttime', 'endtime', 'school', 'major', 'degree', 'content'];

        foreach ($fieldName as $item) {
            $itemInfo[$item] = '';
            if ($request->getParam($item) !== NULL) {
                $itemInfo[$item] = $request->getParam($item);
            }
        }

        $resumeModel = new ResumeModel();

        $result = $resumeModel->saveResumeEdu($oprId, $resumeId, $id, $itemInfo);
        return $this->json($result);
    }

    /**
     * Notes:保存简历头像
     * @param Request $request
     * @return JsonResponse
     */
    public function saveResumeHeadPic(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid');
        $token = $request->getParam('token', '');

        if (empty($resumeId) || empty($token) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $validateRule = [
            'file|附件' => [
                'required',
                'minSize' => 0,
                'maxSize' => 1024 * 1024 * 1,
            ]
        ];

        $file = $request->getFile('file');
        $attachPostfix = strtolower(substr(strrchr($file->getClientOriginalName(), '.'), 1));
        $postfixArr = array("png", "jpg", "jpeg", "gif", "bmp");
        if (!in_array($attachPostfix, $postfixArr)) {
            return $this->json([
                'status' => false,
                'message' => '文件类型' . $attachPostfix . '暂不支持',
            ]);
        }

        $validate = new Validator($validateRule);
        $result = $validate->validate(['file' => $file]);
        if (!$result) {
            return $this->json([
                'status' => false,
                'message' => empty($file->fileName) ? '请上传1M以内的文件' : '文件类型校验失败：' . current($validate->getErrors()),
            ]);
        }

        $resumeModel = new ResumeModel();
        //检测简历是否是RPO简历
        $result = $resumeModel->getResumeBaseInfo($resumeId, 'headpath');
        $headPath_old = '';
        if (empty($result)) {
            return ['status' => false, 'message' => '该简历ID不存在'];
        }
        $headPath_old = $result['headpath'];

        $directory = config('app.file_resumehead.directory');
        $fileHandler = new FileHandler($file);
        $result = $fileHandler->move(true, $directory);
        if (!$result || empty($result['relativepathname'])) {
            return $this->json([
                'status' => false,
                'message' => '上传未知错误',
            ]);
        }
        $headPath_new = $result['relativepathname'];

        $result = $resumeModel->saveResumeHeadPic($oprId, $resumeId, $headPath_new);
        if ($result['status']) {
            if (!empty($headPath_old)) {
                @unlink(FILESERVER . '/' . $headPath_old);
            }
        } else {
            @unlink(FILESERVER . '/' . $headPath_new);
        }
        return $this->json($result);
    }

    /**
     * Notes:删除简历头像
     * @param Request $request
     * @return JsonResponse
     */
    public function delResumeHeadPic(Request $request) {
        $oprId = $this->getLoginId();
        $resumeId = $request->getParam('resumeid');
        $token = $request->getParam('token', '');

        if (empty($resumeId) || empty($token) || $token != UrlCrypt::generateToken([$resumeId])) {
            $resultBody['status'] = false;
            $resultBody['message'] = '参数不合法';
            return $this->json($resultBody);
        }

        $resumeModel = new ResumeModel();

        $result = $resumeModel->delResumeHeadPic($oprId, $resumeId);
        return $this->json($result);
    }

    /**
     * 简历添加沟通记录
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function addResumeContact(Request $request) {
        $resumeId = $request->getPost('resumeid');
        $caseId = $request->getPost('caseid', 0);
        $content = $request->getPost('content', []); // 沟通记录 ，采用数组表示 ，[’field‘=>value]格式
        $resumeRemark = $request->getPost('resumeremark', '');
        $resumeModel = new ResumeModel();
        $result = $resumeModel->addResumeContact($caseId, $resumeId, $content, $resumeRemark);
        return $this->json($result);
    }

    /**
     * 获取简历沟通记录
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function getResumeContact(Request $request) {
        $resumeId = $request->getParam('resumeid');
        $caseId = $request->getParam('caseid', 0);
        $resumeModel = new ResumeModel();
        $result = $resumeModel->getResumeContact($resumeId, $caseId);
        return $this->json($result);
    }

    /**
     * 简历识别出现错误时，客户进行反馈接口
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function reportErrorResume(Request $request) {
        $resumeId = $request->getParam('resumeid');
        $oprId = UserAuth::getLoginId();
        $reason = $request->getPost('reason','');
        $resumeModel = new ResumeModel();
        $result = $resumeModel->reportErrorResume($resumeId,$oprId,$reason);
        return $this->json($result);
    }

    /**
     * Notes:团队下的职位
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function teamCase(Request $request){
        $caseId = $request->getParam('caseid');
        $resumeId = $request->getParam('resumeid');
        $result = (new CaseResume())->getCaseByTeam($resumeId, $caseId);
        return $this->json(
            [
                'status'   => !empty($result),
                'message'  => empty($result) ? '无权限' : '获取成功',
                'caseList' => $result
            ]
        );
    }
}